#!/usr/bin/env python3.9
# Filter kissutil logging output for a time range.
# --
# Chris Baird,, vk2cjb@gmail.com, April 2022

import sys, re, time

timerange = 1200
messages = []

try:
    sys.argv.pop(0)
    timerange = int(sys.argv[0])
except:
    print ("you wanted instructions? oh")
    sys.exit(1)


while True:
    aprsmsg = sys.stdin.readline()
    if aprsmsg == "": break
    g = re.match(r"^\[([0-9]+) (\d+):(\d+):(\d+)\]", aprsmsg)
    if g == None: continue
    aprsmsg = aprsmsg.encode('ascii',errors='ignore').decode().rstrip()
    messages.insert(0, aprsmsg)


t = time.localtime()
hour = int(time.strftime("%H", t))
minute = int(time.strftime("%M", t))
seconds = int(time.strftime("%S", t))
now = 3600*hour + minute*60 + seconds


for aprsmsg in messages:
    g = re.match(r"^\[([0-9]+) (\d+):(\d+):(\d+)\]", aprsmsg)
    g = g.groups()
    pkthour, pktmin, pktsec = int(g[1]), int(g[2]), int(g[3])
    pkttime = 3600*pkthour + 60*pktmin + pktsec

    if (now < pkttime): pkttime -= 86400

    if (pkttime - now) <= timerange and (now - pkttime) <= timerange:
        print (aprsmsg)
    else:
        break

# ----------------------------------------------------------------------
